package com.sihan.framework.etm.controller;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.framework.model.PageResult;
import com.framework.mybatis.Condition;
import com.framework.util.WebUtils;
import com.sihan.framework.etm.common.PageRequest;
import com.sihan.framework.etm.dto.*;
import com.sihan.framework.etm.entity.User;
import com.sihan.framework.etm.entity.business.CollectionRecord;
import com.sihan.framework.etm.mapper.UserMapper;
import com.sihan.framework.etm.mapper.baseData.DeviceMapper;
import com.sihan.framework.etm.service.EtmDeviceMountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.BindException;


@Api(tags = "工器具使用情况统计")
@RestController
@RequestMapping("/device/v2.0")
public class DeviceMountContrller {

    @Autowired
    private EtmDeviceMountService deviceMountService;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/query/usingAstate")
    @ApiOperation("工器具使用次数排名")
    public ApiResult<?> usingAstate() {
        User user = userMapper.selectOne(new Condition<>().eq("username", WebUtils.username()).eq("is_delete", 0));
        String orgId = user.getOrgId();
        return ApiResult.success(deviceMountService.usingAstate(orgId));
    }


    @GetMapping("/query//getDevStateMap")
    @ApiOperation("首页异常记录数量查询")
    public ApiResult<?> getDevStateMap() {

        return ApiResult.success(deviceMountService.getDevStateMap());
    }

    @GetMapping("/query//mountTheAbnormal")
    @ApiOperation("当前时间工器具异常统计")
    public ApiResult<?> mountTheAbnormal() {
        User user = userMapper.selectOne(new Condition<>().eq("username", WebUtils.username()).eq("is_delete", 0));
        String orgId = user.getOrgId();
        return ApiResult.success(deviceMountService.getDevState(orgId));
    }

    @ApiOperation("分页获得单个设备异常记录")
    @GetMapping("/query//pageDeviceOneMount")
    @BusinessLog(value = "分页获得单个设备异常记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id", dataType = "string", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "string"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "string")
    })
    public ApiResult<?> pageDeviceOneMount(MountDTO dto) {
        PageResult<EtmDeviceMountDTO> result = deviceMountService.pageDeviceOneMount(dto);
        return ApiResult.success(result);
    }


    @ApiOperation("分页获得单个设备所有上传记录")
    @GetMapping("/query//deviceOneMount")
    @BusinessLog(value = "分页获得单个设备所有上传记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id", dataType = "string", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", required = true),
            @ApiImplicitParam(name = "mountType", value = "挂载类型 1正常挂载 0 挂载异常", dataType = "int"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "string"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "string")
    })
    @RequiresAuthentication
    public ApiResult<?> deviceOneMount(MountDTO dto,int pageNum,int pageSize) {
        PageResult<EtmDeviceMountDTO> result = deviceMountService.DeviceOneMount(dto,pageNum,pageSize);
        return ApiResult.success(result);
    }


    @ApiOperation("分页获取所有设备上传记录")
    @GetMapping("/query//pageDeviceAllMount")
    @BusinessLog(value = "分页获取所有设备上传记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceName", value = "设备名称", dataType = "string"),
            @ApiImplicitParam(name = "deviceCode", value = "设备编号", dataType = "string"),
            @ApiImplicitParam(name = "mountType", value = "异常状态", dataType = "Integer"),
            @ApiImplicitParam(name = "mountState", value = "挂载状态", dataType = "Integer"),
            @ApiImplicitParam(name = "typeId", value = "设备类型", dataType = "string")
    })
    @RequiresAuthentication
    public ApiResult<?> pageDeviceAllMount(PageRequest pageRequest,
                                           String deviceName,
                                           String deviceCode,
                                           Integer mountType,
                                           Integer mountState,
                                           Integer typeId) {

        PageResult<EtmDeviceMountDTO> result = deviceMountService.pageDeviceAllMount(pageRequest,
                deviceName,
                deviceCode,
                mountType,
                mountState,
                typeId);
        return ApiResult.success(result);
    }


    @ApiOperation("分页获取当前所有设备的异常")
    @GetMapping("/query//pageTheCurrentDeviceAllMount")
    @BusinessLog(value = "分页获取当前所有设备的异常")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceName", value = "设备名称", dataType = "string"),
            @ApiImplicitParam(name = "deviceCode", value = "设备编号", dataType = "string"),
            @ApiImplicitParam(name = "deviceType", value = "设备类型", dataType = "string"),
            @ApiImplicitParam(name = "taskCode", value = "工作票号", dataType = "string")

    })
    @RequiresAuthentication
    public ApiResult<PageResult<GetAllDeviceAlarmDTO>> pageTheCurrentDeviceAllMount(PageRequest pageRequest,
                                                     String deviceName,
                                                     String deviceCode,
                                                     String deviceType,
                                                     String taskCode)throws BindException {

        PageResult<GetAllDeviceAlarmDTO> result = deviceMountService.pageTheCurrentDeviceAllMount(pageRequest,
                deviceName,
                deviceCode,
                deviceType,taskCode);
        return ApiResult.success(result);
    }


    @ApiOperation("分页获取所有设备的电量异常")
    @GetMapping("/query//pageGetAllDevAbnormalElectricQuantity")
    @BusinessLog(value = "分页获取所有设备的电量异常")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceName", value = "设备名称", dataType = "string"),
            @ApiImplicitParam(name = "deviceCode", value = "设备编号", dataType = "string"),
            @ApiImplicitParam(name = "typeId", value = "设备类型", dataType = "string")
    })
    @RequiresAuthentication
    public ApiResult<?> pageGetAllDevAbnormalElectricQuantity(PageRequest pageRequest,
                                                              String deviceName,
                                                              String deviceCode,
                                                              String typeId) {
        PageResult<DeviceRecordDTO> result = deviceMountService.pageGetAllDevAbnormalElectricQuantity(pageRequest,
                deviceName,
                deviceCode, typeId);
        return ApiResult.success(result);
    }


//    @ApiOperation("分页获取单个设备的电量异常")
//    @GetMapping("/query//pageGetOneDevAbnormalElectricQuantity")
//    @BusinessLog(value = "分页获取单个设备的电量异常")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "deviceid", value = "设备id", dataType = "string", required = true)
//    })
//    public ApiResult<?> pageGetOneDevAbnormalElectricQuantity(PageRequest pageRequest,
//                                                              String deviceid) {
//        PageResult<DeviceRecordDTO> result = deviceMountService.pageGetOneDevAbnormalElectricQuantity(pageRequest, deviceid);
//        return ApiResult.success(result);
//    }


    @ApiOperation("分页获取领用记录")
    @GetMapping("/query//getCollectionRecord")
    @BusinessLog(value = "分页获取领用记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceName", value = "设备名称", dataType = "string"),
            @ApiImplicitParam(name = "deviceCode", value = "设备编号", dataType = "string"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", required = true),
            @ApiImplicitParam(name = "state", value = "领用状态", dataType = "int"),
            @ApiImplicitParam(name = "typeId", value = "设备类型", dataType = "string"),
            @ApiImplicitParam(name = "taskCode", value = "工作票号", dataType = "int")
    })
    @RequiresAuthentication
    public ApiResult<?> getCollectionRecord(PageRequest pageRequest,Integer pageNum,Integer pageSize,
                                            String deviceName,
                                            String deviceCode,
                                            String taskCode,
                                            Integer typeId,
                                            Integer state) {

        PageResult<CollectionRecord> result = deviceMountService.getCollectionRecord(pageRequest, pageNum, pageSize,
                deviceCode,
                deviceName,
                taskCode,
                typeId,
                state);
        return ApiResult.success(result);
    }


    @ApiOperation("微信分页获取领用记录")
    @GetMapping("/query/wx/getCollectionRecord")
    @BusinessLog(value = "微信分页获取领用记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", value = "设备编号", dataType = "string"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", required = true),
            @ApiImplicitParam(name = "state", value = "领用状态", dataType = "string"),
            @ApiImplicitParam(name = "currentState", value = "当前状态", dataType = "string")
    })
    @RequiresAuthentication
    public ApiResult<?> wxGetCollectionRecord(Integer pageNum,Integer pageSize,
                                            String deviceCode,
                                            String taskCode,
                                            String state,
                                              String currentState) {

        PageResult<CollectionRecord> result = deviceMountService.wxGetCollectionRecord( pageNum, pageSize,
                deviceCode,
                state,currentState);
        return ApiResult.success(result);
    }


    @ApiOperation("根据设备id获取设备领用记录")
    @GetMapping("/query//getByIdRecord")
    @BusinessLog(value = "根据设备id获取设备领用记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备Id", dataType = "string", required = true),
            @ApiImplicitParam(name = "taskCode", value = "工作票code", dataType = "string"),
            @ApiImplicitParam(name = "startTheReturn", value = "归还开始时间", dataType = "string"),
            @ApiImplicitParam(name = "endTheReturn", value = "归还结束时间", dataType = "string"),
            @ApiImplicitParam(name = "state", value = "领用状态", dataType = "int")
    })
    public ApiResult<PageResult<GetByIdRecordDTO>> getByIdRecord(PageRequest pageRequest, String startTheReturn, String endTheReturn,
                                                     String deviceId, String taskCode, Integer state) {

        PageResult<GetByIdRecordDTO> result = deviceMountService.getByIdRecord(pageRequest,
                startTheReturn,
                endTheReturn,
                deviceId,
                taskCode,
                state);
        return ApiResult.success(result);
    }





}
